import React from "react";
import ProForm, { ProFormText } from "@ant-design/pro-form";
import { addUser } from '@/services/ant-design-pro/user';
import { Modal, message } from 'antd'

const Create = (props) => {

    const { isModalAdd, isShowModalAdd, actionRef, setUserData } = props;

    /**
     * 添加用户
     * @param {obj} params 参数
     * @returns 
     */
    const createUser = async (params) => {
        let result = await addUser(params);
        if (result.status == 'ok') {
            await setUserData();
            actionRef.current.reload();
            message.success("添加成功");
        } else {
            message.error('添加失败：' + result.data.message);
        }
        isShowModalAdd(false);
    }

    return (
        <Modal title="添加用户"
            footer={false}
            visible={isModalAdd}
            onCancel={() => isShowModalAdd(false)}
            destroyOnClose={true}>
            <ProForm
                onFinish={(values) => createUser(values)}
            >
                <ProFormText
                    name="username"
                    label="昵称"
                    placeholder="请输入名称"
                    rules={[
                        { required: true, message: "请输入昵称" }
                    ]}
                />
                <ProFormText
                    name="email"
                    label="邮箱"
                    placeholder="请输入邮箱"
                    rules={[
                        { required: true, message: "请输入邮箱" }
                    ]}
                />
                <ProFormText.Password
                    name="password"
                    label="密码"
                    placeholder="请输入密码"
                    rules={[
                        { required: true, message: "请输入密码" },
                        { min: 6, message: '密码最小6位' }
                    ]}
                />
            </ProForm>
        </Modal>
    );
}

export default Create;